Skip to content

quad: Import sympy only when necessary - #459

Merged
mmcky merged 4 commits into
QuantEcon:masterfrom
rht:import
Dec 14, 2018
Merged

mmcky merged 4 commits into
QuantEcon:masterfrom
rht:import

Conversation

@rht

@rht rht commented Dec 11, 2018 •

Copy link
Copy Markdown
Contributor

This partially addresses fixes #449. The import quantecon time on my machine decreases from ~5.4s to 4.6s to ~1s.

@rht

rht commented Dec 11, 2018

Copy link
Copy Markdown
Contributor Author

With the second commit, it goes from ~4.6s to ~2s.

@coveralls

coveralls commented Dec 11, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage decreased (-0.01%) to 94.538% when pulling e265b25 on rht:import into ab0c261 on QuantEcon:master.

@rht

rht commented Dec 11, 2018

Copy link
Copy Markdown
Contributor Author

nose-timer stat:

[success] 14.59% quantecon.tests.test_gridtools.test_performance_C: 31.6635s
[success] 10.93% quantecon.tests.test_gridtools.test_performance_F: 23.7086s
[success] 3.17% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_bohachevsky: 6.8868s
[success] 2.98% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_zakharov: 6.4733s
[success] 2.66% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_sum_squared: 5.7616s
[success] 2.50% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_styblinski_tang: 5.4286s
[success] 2.42% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_perm_function: 5.2586s
[success] 2.42% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_easom: 5.2527s
[success] 2.35% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_rotated_hyper_ellipsoid: 5.1085s
[success] 2.28% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_goldstein_price: 4.9372s
[success] 2.26% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_powell: 4.9080s
[success] 2.20% quantecon.util.tests.test_numba.TestNumbaLinalgSolve.test_b_1dim: 4.7764s
[success] 2.18% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_rosenbrock: 4.7321s
[success] 2.16% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_corner_sol: 4.6921s
[success] 2.13% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_colville: 4.6129s
[success] 2.12% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_mccormick: 4.6027s
[success] 2.10% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_booth: 4.5612s
[success] 2.09% quantecon.optimize.tests.test_nelder_mead.TestMaximization.test_discontinuous: 4.5425s
[success] 1.75% quantecon.optimize.tests.test_nelder_mead.test_invalid_σ: 3.7913s
[success] 1.74% quantecon.optimize.tests.test_nelder_mead.test_invalid_ρχ: 3.7754s
[success] 1.71% quantecon.optimize.tests.test_nelder_mead.test_invalid_γ: 3.7168s
[success] 1.63% quantecon.optimize.tests.test_nelder_mead.test_invalid_ρ: 3.5275s
[success] 1.60% quantecon.optimize.tests.test_nelder_mead.test_invalid_χ: 3.4731s
[success] 1.34% quantecon.util.tests.test_numba.TestNumbaLinalgSolve.test_b_2dim: 2.9008s
[success] 1.26% test_lemke_howson.TestLemkeHowson.test_lemke_howson: 2.7230s
[success] 1.20% test_repeated_game.TestAS.test_abreu_sannikov: 2.5982s
[success] 1.15% test_random.TestRandomDiscreteDP.test_equal_beta: 2.5023s
[success] 1.15% quantecon.tests.test_inequality.test_lorenz_curve: 2.4939s
[success] 1.07% test_mclennan_tourky.TestEpsilonNash.test_epsilon_nash_with_full_output: 2.3290s
[success] 0.94% test_random.test_random_stochastic_matrix_dense_vs_sparse: 2.0447s
[success] 0.86% quantecon.tests.test_compute_fp.TestComputeFPContraction.test_num_iter_large: 1.8700s
[success] 0.83% quantecon.optimize.tests.test_nelder_mead.test_invalid_bounds_1: 1.7947s
[success] 0.78% test_support_enumeration.TestSupportEnumeration.test_no_error_skew_sym: 1.6925s
[success] 0.76% quantecon.tests.test_lss.TestLinearStateSpace.test_replicate: 1.6590s
[success] 0.69% test_support_enumeration.TestSupportEnumeration.test_support_enumeration: 1.5076s
[success] 0.67% test_utilities.test_sample_without_replacement_shape: 1.4527s
[success] 0.66% test_utilities.TestProbvec.test_parallel_cpu: 1.4344s
[success] 0.64% quantecon.tests.test_inequality.test_gini_coeff: 1.3894s
[success] 0.58% test_random.test_random_markov_chain_dense: 1.2546s
[success] 0.58% test_normal_form_game.test_player_corner_cases: 1.2525s
[success] 0.53% test_random.TestRandomDiscreteDP.test_shape: 1.1552s
[success] 0.53% test_utilities.TestProbvec.test_shape: 1.1430s
[success] 0.49% test_vertex_enumeration.TestVertexEnumeration.test_vertex_enumeration: 1.0575s
[success] 0.48% test_random.TestRandomDiscreteDP.test_nonzero: 1.0440s
[success] 0.48% quantecon.tests.test_compute_fp.TestComputeFPContraction.test_2d_input: 1.0360s
[success] 0.48% test_random.TestRandomDiscreteDP.test_equal_reward: 1.0338s
[success] 0.48% test_random.TestRandomDiscreteDP.test_equal_probability: 1.0319s
[success] 0.46% quantecon.util.tests.test_timing.TestTicTacToc.test_loop: 1.0033s
[success] 0.44% test_bimatrix_generators.TestTournamentGame.test_payoff_values: 0.9591s
[success] 0.43% test_ddp.test_ddp_beta_0: 0.9365s
[success] 0.36% quantecon.optimize.tests.test_root_finding.test_brentq_basic: 0.7826s
[success] 0.36% quantecon.optimize.tests.test_scalar_max.test_brent_max: 0.7812s
[success] 0.36% test_ddp.TestDiscreteDP.test_modified_policy_iteration: 0.7807s
[success] 0.35% quantecon.optimize.tests.test_root_finding.test_newton_basic: 0.7537s
[success] 0.33% quantecon.optimize.tests.test_scalar_max.test_invalid_a_brent_max: 0.7132s
[success] 0.33% test_normal_form_game.test_best_response_2p: 0.7081s
[success] 0.33% test_random.test_random_stochastic_matrix_sparse: 0.7054s
[success] 0.32% test_random.test_random_stochastic_matrix_dense: 0.6928s
[success] 0.30% test_random.test_random_markov_chain_sparse: 0.6608s
[success] 0.28% quantecon.optimize.tests.test_scalar_max.test_invalid_a_b_brent_max: 0.6117s
[success] 0.28% quantecon.optimize.tests.test_root_finding.test_newton_hard: 0.6071s
[success] 0.27% test_bimatrix_generators.TestBlottoGame.test_constant_diagonal: 0.5965s
[success] 0.26% quantecon.optimize.tests.test_scalar_max.test_invalid_b_brent_max: 0.5594s
[success] 0.22% quantecon.optimize.tests.test_root_finding.test_bisect_basic: 0.4668s
[success] 0.21% quantecon.optimize.tests.test_root_finding.test_halley_hard: 0.4654s
[success] 0.20% quantecon.optimize.tests.test_root_finding.test_halley_basic: 0.4394s

@rht

rht commented Dec 11, 2018

Copy link
Copy Markdown
Contributor Author

test_nelder_mead alone took 46.45% of the entire time. Since Travis can have 4 concurrent jobs, it would be possible to split the test into the cartesian product of [linux, darwin] and [test_nelder_mead, the rest].

@oyamad

oyamad commented Dec 11, 2018

Copy link
Copy Markdown
Member

Do we really need these tests

[success] 14.59% quantecon.tests.test_gridtools.test_performance_C: 31.6635s
[success] 10.93% quantecon.tests.test_gridtools.test_performance_F: 23.7086s

?

Ref #109

@oyamad oyamad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change (in the second commit) makes one of the _probvec_* functions complied for every call, which I am afraid is inefficient.

@oyamad

oyamad commented Dec 11, 2018 •

Copy link
Copy Markdown
Member

Adding target='parallel' cache=True (CORRECTED) to the _probvec_* functions reduced the import time from 1.922s to 1.003s on the second time import on my machine.

@rht

rht commented Dec 11, 2018

Copy link
Copy Markdown
Contributor Author

OK, I removed the guvectorize commit. I suppose I should have timed the hot start that has the numba caches ready.

@rht

rht commented Dec 11, 2018

Copy link
Copy Markdown
Contributor Author

Adding target='parallel' to the _probvec_* functions reduced the import time from 1.922s to 1.003s on the second time import on my machine.

@oyamad did you mean cache=True?

@rht

rht commented Dec 11, 2018

Copy link
Copy Markdown
Contributor Author

On quantecon.arma, either:

  1. not put it on the top-level name space (scipy.signal is not at the scipy namespace either, https://github.com/scipy/scipy/blob/master/scipy/__init__.py)
  2. lazy-import the scipy.signal at the class initialization

@oyamad

oyamad commented Dec 11, 2018

Copy link
Copy Markdown
Member

@rht Yes, cache=True, sorry missed copy-paste...

@oyamad

oyamad commented Dec 12, 2018

Copy link
Copy Markdown
Member

+1 for 2. lazy import of scipy.signal.

Comment thread quantecon/random/utilities.py Resolved
@rht

rht commented Dec 12, 2018

Copy link
Copy Markdown
Contributor Author

OK, the import time is now at ~1s on my machine,
2018-12-12-110234_1863x900_scrot
Other than the numba import, the rest consists of the cached generated_jit and guvectorize, which I suppose can't be optimized further.

@oyamad oyamad left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done, this is very helpful!

@mmcky

mmcky commented Dec 13, 2018

Copy link
Copy Markdown
Contributor

thanks @oyamad and @rht this is really great work! That's a pretty neat tool.

@mmcky

mmcky commented Dec 13, 2018

Copy link
Copy Markdown
Contributor

I think selective imports works well -- when done like this. Is there any PEP guidance to this?

@rht

rht commented Dec 13, 2018

Copy link
Copy Markdown
Contributor Author

The only article I found documenting various options to do lazy import: https://snarky.ca/lazy-importing-in-python-3-7/ (note: the author wrote importlib.util.LazyLoader for the stdlib, python/cpython@a04dbe4). From the article, I suppose the demand for the functionality was substantial enough to have it formalized in importlib instead of resorting the local imports method I have used here.

@rht

rht commented Dec 13, 2018

Copy link
Copy Markdown
Contributor Author

See also: numpy/numpy#11457.

@mmcky

mmcky commented Dec 14, 2018

Copy link
Copy Markdown
Contributor

thanks @rht - this is a great improvement! Also thanks for those links. Lazy importing makes a lot of sense.

@mmcky
mmcky merged commit 0a34310 into QuantEcon:master Dec 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Startup time is slow

4 participants